<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Employees_db extends CI_Model
{
	function getEmployee($data=false, $id=false)
	{
		if($data)
			$this->db->where($data);
		if($id)
			$this->db->where('id != ',$id);
		$this->db->where('a.is_deleted = 0');
		$this->db->select("a.*, b.position_id, b.position_name, c.group_id, c.group_name, d.es_name, e.gender_name");
		$this->db->join("positions b","b.position_id = a.position_id", "left");
		$this->db->join("groups c","c.group_id = b.group_id", "left");
		$this->db->join("employee_status d","d.employee_status_id = a.employee_status_id", "left");
		$this->db->join("genders e","e.gender_id = a.gender_id", "left");
		$query = $this->db->get('users a');
		return $query->result_array();
	}
	
	function getBirthdays($data=false)
	{
		if($data)
			$this->db->where($data);
		$this->db->select("id, MONTH(birthday) as month, DAY(birthday) as day, firstname, lastname");
		$this->db->where('is_deleted = 0');
		$query = $this->db->get('users');
		return $query->result_array();
	}
	
	function getEmployeeMoreDetails($data=false)
	{
		if($data)
			$this->db->where($data);
		$this->db->where('a.is_deleted = 0');
		$this->db->select("a.*, b.position_id, b.position_name, c.group_id, c.group_name, e.fullname as manager");
		$this->db->join("positions b","b.position_id = a.position_id", "left");
		$this->db->join("groups c","c.group_id = b.group_id", "left");		
		$this->db->join("positions d","d.group_id = c.parent_group_id", "left");
		$this->db->join("users e","e.position_id = d.position_id", "left");
		$query = $this->db->get('users a');
		return $query->row_array();
	}
	
	function validateUniqueValues($employee_no, $username, $email, $id = false)
	{
		if($id)
			$this->db->where('id != ',$id);
		$this->db->where("( employee_no = '$employee_no' OR username = '$username' OR email = '$email' )");
		//$this->db->or_where('username',$username);
		//$this->db->or_where('email',$email);
		$this->db->where('is_deleted = 0');
		$query = $this->db->get('users');
		return $query->result();
	}
	
	function save($data, $item=false)
	{	
		if (!$item)
		{			
			$this->db->trans_start();
			$this->db->insert('users', $data);
			$item = $this->db->insert_id();
			$this->db->trans_complete(); 					
		}
		else
		{
			$this->db->trans_start();
			$this->db->where('id', $item);
			$this->db->update('users', $data);		
			$this->db->trans_complete();
		}
		return $item;	
	}
	
	function delete($id)
	{
		if(count($id)>1)
			$this->db->where_in('id',$id);
		else
			$this->db->where('id',$id[0]);
		//$this->db->delete('users');
		$this->db->update('users', array('is_deleted'=>1));
		return $id;
	}
	
	function getList()
	{		
		$sortname = $this->input->post('sortname');
		$sortorder = $this->input->post('sortorder');
		$page = $this->input->post('page');
		$rp = $this->input->post('rp');		
		
		//$query = $this->input->post('query');
		//$qtype = $this->input->post('qtype');
		$squery = $this->input->post('squery');
		
		$cond = "";

		if (!$sortname) $sortname = 'fullname';
		if (!$sortorder) $sortorder = 'ASC';
		
		
		if (!$page) $page = 1;
		if (!$rp) $rp = 40;
		
		$start = (($page-1) * $rp);
		
		$this->db->start_cache();
		if ($squery) $this->db->where("(LOWER(a.fullname) LIKE '%$squery%' OR LOWER(a.employee_no) LIKE '%$squery%' OR LOWER(a.username) LIKE '%$squery%')");
		$this->db->where('a.is_deleted = 0');
		$this->db->select("a.id as pkey, a.id as test, a.employee_no, a.fullname, a.username, a.email, b.position_name");
		$this->db->join("positions b","b.position_id = a.position_id", "left");
		$this->db->order_by($sortname,$sortorder);
		$this->db->limit($rp, $start);		
		$query = $this->db->get("users a");
		//$num = $this->db->count_all_results();
		$num = count($query->result_array());

		$this->db->flush_cache();
		
		
		$rows = array();
		if (isset($query))
		{			 
			foreach($query->result_array() as $row):			
				$key = key($row);
				$id = $row[$key];
				unset($row[$key]);
				$cell = array();
				foreach ($row as $item) { $cell[] = $item; }
				$rows[] = array( "id" => $id, "cell" => $cell );				
			endforeach; 
		}
		
		$data['rows'] = $rows;
		$data['page'] = $page;
		$data['total'] = $num;
		return $data;			
		
	}

}

?>